using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;
using NetProtocol;
using NetProtocol.POD;

namespace IQIGame.Onigao.GamePlay
{
    public static partial class NetProtocolHelper
    {
        /// <summary>
        /// 开始网络调用, 
        /// </summary>
        public delegate void BeginCallRequest(int messageId);
        public static BeginCallRequest beginCallRequest;
        /// <summary>
        /// 结束网络调用
        /// </summary>
        /// <param name="messageId"></param>
        /// <param name="code"></param>
        public delegate void EndCallRequest(int messageId, int code);
        public static EndCallRequest endCallRequest;

        public static void Init()
        {
            ItemPOD.DynamicFactory = PODPool<ItemPOD>.Init(10);
            ItemShowPOD.DynamicFactory = PODPool<ItemShowPOD>.Init(10);
            HeroPOD.DynamicFactory = PODPool<HeroPOD>.Init(10);
            QuestPOD.DynamicFactory = PODPool<QuestPOD>.Init(10);
            UpdatePlayerInfoPOD.DynamicFactory = PODPool<UpdatePlayerInfoPOD>.Init(2);
            SChatPOD.DynamicFactory = PODPool<SChatPOD>.Init(5);
            PlayerSimplePOD.DynamicFactory = PODPool<PlayerSimplePOD>.Init(5);
            BattleUpdatePOD.DynamicFactory = PODPool<BattleUpdatePOD>.Init(20);
            LevelEntityPOD.DynamicFactory = PODPool<LevelEntityPOD>.Init(10);
            LevelBuffChangePOD.DynamicFactory = PODPool<LevelBuffChangePOD>.Init(10);
            PetPOD.DynamicFactory = PODPool<PetPOD>.Init(16);

            RequestSaveLevelPOD.Serial_saveData = (saveData, stream) =>
            {
                LevelSavePOD levelSavePOD = (LevelSavePOD)saveData;
                stream.Write_bytes(((IPOD)levelSavePOD).Serial());
            };

            CreateLevelPOD.UnSerial_saveData = stream =>
            {
                byte[] data = stream.Read_bytes();
                if (data == null)
                {
                    return null;
                }
                LevelSavePOD levelSavePOD = new LevelSavePOD();
                ((IPOD)levelSavePOD).UnSerial(data);
                return levelSavePOD;
            };

            GetLevelSavePOD.UnSerial_saveData = stream =>
            {
                byte[] data = stream.Read_bytes();
                if (data == null)
                {
                    return null;
                }
                LevelSavePOD levelSavePOD = new LevelSavePOD();
                ((IPOD)levelSavePOD).UnSerial(data);
                return levelSavePOD;
            };

            BattleUpdatePOD.Serial_data = IQIGame.Onigao.Logic.BattleUpdateFactory.SerializeCommand;
            BattleUpdatePOD.UnSerial_data = IQIGame.Onigao.Logic.BattleUpdateFactory.TryParseUpdate;
        }

        public static SendPackage GetSendPackage()
        {
            return ConcurrentClassPool<SendPackage>.Get();
        }

        public static void ReturnSendPackage(SendPackage sendPackage)
        {
            ConcurrentClassPool<SendPackage>.Put(sendPackage);
        }

        public static void ClientLog(int messageId, string message)
        {
            switch (messageId)
            {
                case NetMessageId.gameServer_ping: break;
                case NetMessageId.gameServer_pang: break;
                case NetMessageId.logicServer_ping: break;
                case NetMessageId.logicServer_pang: break;
                case NetMessageId.levelLogic_Move: break;
                case NetMessageId.levelLogic_NotifySyncPosition: break;
                case NetMessageId.levelLogic_UpdateEntities: break;
                default: UnityEngine.Debug.Log(message); break;
            }

        }

    }
}
